package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import util.JDBCUtil;
import util.NumberUtil;

/**
 * Servlet implementation class ShowAverageServlet
 */
@WebServlet("/ShowAverageServlet")
public class ShowAverageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowAverageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		Connection con = null;
		PreparedStatement pStatement = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "select * from study";
		ArrayList<ArrayList<Object>> ans;
		ArrayList<Object> cowList;
		int midtermAllScore=0;
		int terminalAllScore=0;
		int ptaAllScore=0;
		int MOOCAllScore=0;
		int finalAllScore=0;
		double midtermAver=0;
		double terminalAver=0;
		double ptaAver=0;
		double MOOCAver=0;
		double finalScoreAver=0;
		try {
			int num = 0;
			con = JDBCUtil.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()) {
				for(int i = 7; i <= 11; i++) {
					String s = rs.getString(i);
					if(NumberUtil.isNumber(s)) {
						switch(i){
						case 7:
							midtermAllScore += Integer.parseInt(s);
							break;
						case 8:
							terminalAllScore += Integer.parseInt(s);
							break;
						case 9:
							ptaAllScore += Integer.parseInt(s);
							break;
						case 10:
							MOOCAllScore += Integer.parseInt(s);
							break;
						case 11:
							finalAllScore += Integer.parseInt(s);
							break;
						default:
							break;
						}
					}
				}
				num++;
			}
			DecimalFormat   df   = new  DecimalFormat("#.00"); 
			midtermAver =Double.parseDouble(df.format((double)midtermAllScore/num));  //计算平均分
			terminalAver =Double.parseDouble(df.format((double)terminalAllScore/num));
			ptaAver = Double.parseDouble(df.format((double)ptaAllScore/326*100/num));
			MOOCAver = Double.parseDouble(df.format((double) MOOCAllScore/238*100/num));
			finalScoreAver = Double.parseDouble(df.format((double) finalAllScore/num));
			Map<String, Object> map = new HashMap<>();
			double[] score = {midtermAver,terminalAver,ptaAver,MOOCAver,finalScoreAver};
			double[] newScore = {midtermAver,terminalAver,ptaAver/100*326,MOOCAver/100*238,finalScoreAver};
			this.getServletContext().setAttribute("averScore",newScore);
			String[] exam = {"期中成绩","期末成绩","pta成绩","MOOC成绩","最终成绩"};
			map.put("score",score);
		    map.put("exam", exam);
		    response.getWriter().println(JSON.toJSONString(map));  //通过json传递exam，和学生平均分
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.realeaseAll(rs, stmt, con, pStatement);
		}
	}
}
